//
//  FKHomeArticleCell.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/16.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKHomeArticleCell.h"
#import "UUMarqueeView.h"

@interface FKHomeArticleCell ()<UUMarqueeViewDelegate>

@property (nonatomic, weak) UUMarqueeView *marqueeView;
//跑马灯数组
@property (nonatomic, strong) NSArray *marqueeDatas;

@end

@implementation FKHomeArticleCell

- (void)setupSubviews
{
    [super setupSubviews];
    self.backgroundColor = [UIColor whiteColor];
    
    // 公告
    UIImageView *gonggaoImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_news"]];
    [self.contentView addSubview:gonggaoImg];
    [gonggaoImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(fk_adjustW(43));
        make.height.offset(fk_adjustW(39));
        make.left.offset(12);
        make.centerY.offset(0);
    }];
    
    UIView *line = [UIView fk_lineWithFrame:CGRectZero];
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(gonggaoImg.mas_right).offset(8);
        make.width.offset(1);
        make.height.offset(fk_adjustW(24));
        make.centerY.offset(0);
    }];
    
    //    UUMarqueeView *marqueeView = [[UUMarqueeView alloc] initWithFrame:CGRectMake(60, 5, SCREENWIDTH-50, 50)];
    UUMarqueeView *marqueeView = [[UUMarqueeView alloc] initWithFrame:CGRectZero];
    [self.contentView addSubview: marqueeView];
    self.marqueeView = marqueeView;
    marqueeView.delegate = self;
    marqueeView.timeIntervalPerScroll = 2.0f;
    marqueeView.timeDurationPerScroll = 2.0f ;
    marqueeView.touchEnabled = YES;
    marqueeView.backgroundColor = [UIColor whiteColor];
    [marqueeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(2);
        make.bottom.offset(-2);
        make.left.equalTo(line.mas_right).offset(8);
        make.right.offset(0);
    }];
}

#pragma mark - UUMarqueeViewDelegate
- (NSUInteger)numberOfDataForMarqueeView:(UUMarqueeView*)marqueeView;
{
    return self.marqueeDatas.count;
}

- (NSUInteger)numberOfVisibleItemsForMarqueeView:(UUMarqueeView*)marqueeView
{
    return 2;
}

- (NSArray*)dataSourceArrayForMarqueeView:(UUMarqueeView*)marqueeView
{
    return self.marqueeDatas;
}

- (void)createItemView:(UIView*)itemView forMarqueeView:(UUMarqueeView*)marqueeView
{
    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_news_horn"]];
    [itemView addSubview:iconView];
    
    UILabel *contentLabel = [UILabel fk_labelWithFont:fk_adjustFont(13) textColor:fkColor666666];
    [itemView addSubview:contentLabel];
    contentLabel.tag = 10;
    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(12));
        make.left.offset(0);
        make.centerY.offset(0);
    }];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconView.mas_right).offset(6);
        make.centerY.offset(0);
    }];
}

- (void)updateItemView:(UIView*)itemView atIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView*)marqueeView
{
    UILabel *content = [itemView viewWithTag:10];
    content.text = self.marqueeDatas[index][@"title"];
}

- (void)didTouchItemViewAtIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView*)marqueeView
{
    if ([self.delegate respondsToSelector:@selector(homeArticleCell:didClickedArticle:)]) {
        NSDictionary *dict = self.marqueeDatas[index];
        [self.delegate homeArticleCell:self didClickedArticle:dict[@"id"]];
    }
}

- (void)setData:(id)data
{
    [super setData:data];
    
    self.marqueeDatas = data;
    [self.marqueeView reloadData];
}


@end
